package testDriver;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class TestAddStation {

	/**
	 * @param args
	 * @throws IOException
	 * @throws HttpException
	 */
	public static void main(String[] args) throws HttpException, IOException {

		final String stationToAdd = 
				"<station>" + 
						  "<id>99999</id>"
						+ "<isOpen>0</isOpen>" 
					    + "<latitude>10.0</latitude>"
					    + "<longitude>10.0</longitude>" 
					    + "<valid>false</valid>"
					    + "<velibid>0</velibid>" 
					    + "<version>0</version>"
		     + "</station>";
		
		
		try {
			 
			Client client = Client.create();
	 
			WebResource webResource = client
			   .resource("http://localhost:8080/Aar3/rest/station/post");
	 
			ClientResponse response = webResource.type("application/xml")
			   .post(ClientResponse.class, stationToAdd);
	 
			if (response.getStatus() != 201) {
				throw new RuntimeException("Failed : HTTP error code : "
				     + response.getStatus());
			}
	 
			System.out.println("Output from Server .... \n");
			String output = response.getEntity(String.class);
			System.out.println(output);
	 
		  } catch (Exception e) {
	 
			e.printStackTrace();
	 
		  }

//		PostMethod postMethod = new PostMethod(
//				"http://localhost:8080/Aar3/rest/station");
//		RequestEntity entity = new InputStreamRequestEntity(
//				new ByteArrayInputStream(addStationXml.getBytes()),
//				"application/xml");
//		postMethod.setRequestEntity(entity);
//		HttpClient client = new HttpClient();
//		try {
//			int result = client.executeMethod(postMethod);
//			System.out.println("Response status code: " + result);
//			System.out.println("Response headers:");
//			Header[] headers = postMethod.getResponseHeaders();
//			for (int i = 0; i < headers.length; i++)
//				System.out.println(headers[i].toString());
//		} finally {
//			postMethod.releaseConnection();
//		}
	}

}
